package calculators;

/**
 * Performs windowing actions on the selected samples
 * 
 * @author Peter Schilling, Jeff Hunter
 */
public class Windowing {

	public static final double TWO_PI = Math.PI * 2;

	public static double[] triangular(double samples[]) {
		for (int i = 0; i < samples.length; i++)
			samples[i] *= (double) (2d / samples.length * (samples.length / 2d - Math.abs(i - (samples.length - 1) / 2d)));
		return samples;
	}
	
	public static double[] triangular(byte[] samples) {
		double[] dsamples = new double[samples.length];
		for (int i = 0; i < dsamples.length; i++)
			dsamples[i] = (((int) samples[i] & 0x000000FF) - 128);
		return triangular(dsamples);
	}

	public static double[] hamming(double samples[]) {
		for (int i = 0; i < samples.length; i++)
			samples[i] *= (0.54f - 0.46f * Math.cos(TWO_PI * i / (samples.length - 1)));
		return samples;
	}
	
	public static double[] hamming(byte[] samples) {
		double[] dsamples = new double[samples.length];
		for (int i = 0; i < dsamples.length; i++)
			dsamples[i] = (((int) samples[i] & 0x000000FF) - 128);
		return hamming(dsamples);
	}
}
